import { Point } from './ts-geometry-2d/Point';
import { TableAtrribute } from '../core/TableAttribute';
import { DeskBall, HitPath, AiGTool } from './aicore';
import { Vector } from './ts-geometry-2d/Vector';

// AI击球思考的更像人类
export class HitSetSpin {
  private aiLevel = 0; // L1 L2 L3 L4 L5
  constructor(lv: number) { this.aiLevel = lv; }
  // 球桌距离最大值的平方, 斜对角线的最大值
  public hitSpin(mainBall: DeskBall, hitPaths: Array<HitPath>) {
    if (hitPaths.length > 0) {
      this.setHitSpin(mainBall, hitPaths[0]);
    }
  }

  // 最简单的加塞操作
  private setHitSpin(mainBall: DeskBall, path: HitPath) {
    // 低塞： 1. 目标球距离球洞很近  2. 白球，目标球，球洞 在一条直线上
    if (path.hitType !== HitPath.HitTypeDirect) {
      return;
    }
    if (!path.targetHole || !path.targetBall) {
      return;
    }
    const dista = path.targetHole.position.distanceSquare(path.targetBall.position);
    const distb = mainBall.position.distanceSquare(path.targetBall.position);
    const thres = TableAtrribute.DeskHalfAxisZLength * TableAtrribute.DeskHalfAxisZLength;
    if (dista > thres && distb > thres) {
      return;
    }
    
    // 三点共线
    const lowSpin = AiGTool.pointOnLine(mainBall.position, path.targetBall.position, path.targetHole.position, 1.0);
    if (lowSpin) { path.setSpin(0, -0.4); }
  }
}

